<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" destroy-on-close :close-on-click-modal="false"
        :before-close="closeDailog" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="文件空间主键" prop="pkFileSpace">
                <ec-select source="fileSpace" v-model="dataForm.pkFileSpace" placeholder="文件空间主键"></ec-select>
            </el-form-item>
            <el-form-item label="文件名" prop="fileName">
                <el-input v-model="dataForm.fileName" placeholder="文件名"></el-input>
            </el-form-item>
            <el-form-item label="文件类型" prop="fileType">
                <el-input v-model="dataForm.fileType" placeholder="文件类型"></el-input>
            </el-form-item>
            <el-form-item label="存储类型" prop="storeType">
                <ec-dict-select v-model="dataForm.storeType" placeholder="存储类型" dictType="store_type"></ec-dict-select>
            </el-form-item>
            <el-form-item label="文件大小" prop="fileSize">
                <el-input v-model="dataForm.fileSize" placeholder="文件大小"></el-input>
            </el-form-item>
            <el-form-item label="文件路径" prop="filePath">
                <el-input v-model="dataForm.filePath" placeholder="文件路径"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as filemanagerService from '@/api/file/filemanager'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkFile: 0,
                    pkFileSpace: '',
                    fileName: '',
                    fileType: '',
                    storeType: '',
                    fileSize: '',
                    filePath: '',
                    isDelete: '',
                    revision: '',
                },
                dataRule: {
                    pkFileSpace: [{
                        required: true,
                        message: '文件空间主键不能为空',
                        trigger: 'blur'
                    }],
                    fileName: [{
                        required: true,
                        message: '文件名不能为空',
                        trigger: 'blur'
                    }],
                    fileType: [{
                        required: true,
                        message: '文件类型不能为空',
                        trigger: 'blur'
                    }],
                    storeType: [{
                        required: true,
                        message: '存储类型不能为空',
                        trigger: 'blur'
                    }],
                    fileSize: [{
                        required: true,
                        message: '文件大小不能为空',
                        trigger: 'blur'
                    }],
                    filePath: [{
                        required: true,
                        message: '文件路径不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkFile = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkFile) {
                        filemanagerService.getfilemanagerinfo(this.dataForm.pkFile).then(data => {
                            this.dataForm.pkFileSpace = data.pkFileSpace
                            this.dataForm.fileName = data.fileName
                            this.dataForm.fileType = data.fileType
                            this.dataForm.storeType = data.storeType
                            this.dataForm.fileSize = data.fileSize
                            this.dataForm.filePath = data.filePath
                            this.dataForm.isDelete = data.isDelete
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkFile': this.dataForm.pkFile || undefined,
                            'pkFileSpace': this.dataForm.pkFileSpace,
                            'fileName': this.dataForm.fileName,
                            'fileType': this.dataForm.fileType,
                            'storeType': this.dataForm.storeType,
                            'fileSize': this.dataForm.fileSize,
                            'filePath': this.dataForm.filePath,
                            'isDelete': this.dataForm.isDelete,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkFile ? filemanagerService.addfilemanager(params) :
                            filemanagerService.updatefilemanager(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>